package com.xfly.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.OrderBean;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.OrderListAdapter;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.adapter.OffLineOrderListAdapter;
import com.xfly.app.bean.KcBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OfflineOrderActivity extends BaseMvpActivity {


    PullableListView mListview;
    public PullToRefreshLayout mRefreshView;
    TitleBarView title_bar;
    int page = 1;
    List<OrderBean> mDataLists = new ArrayList<>();
    OffLineOrderListAdapter mOrderListAdapter;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    Button tosubgood;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_offlineorder);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        mRefreshView = findViewById(R.id.refresh_view);
        mListview = findViewById(R.id.listview);
        tosubgood = findViewById(R.id.tosubgood);

        title_bar.setText("线下订单");
    }

    @Override
    public void initEvents() {
        mOrderListAdapter = new OffLineOrderListAdapter(getContext(), mDataLists);
        mListview.setAdapter(mOrderListAdapter);
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });


        mRefreshView.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page++;
                getData();
            }
        });

        tosubgood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(),SubListActivity.class));
            }
        });
        getData();

    }


    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("mold", "2");
        map.put("seller", "1");

        apiDataRepository.getOrderList(map, new ApiNetResponse<List<OrderBean>>() {
            @Override
            public void onSuccess(List<OrderBean> datas) {
                if (null != mRefreshView) {
                    mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if (page == 1) {
                    mDataLists.clear();
                    if (null != datas && datas.size() > 0) {
                        mDataLists.addAll(datas);
                    } else {
                    }
                } else {
                    if (null != datas && datas.size() > 0) {
                        mDataLists.addAll(datas);
                    } else {
                        showMsg(R.string.has_no_more);
                    }
                }
                mOrderListAdapter.pushData(mDataLists);
            }

            @Override
            public void onFaile(String info, List<OrderBean> data, Throwable throwable) {
                super.onFaile(info, data, throwable);
                if (null != mRefreshView) {
                    mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
            }
        });
    }
}
